home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / debugsh.c < prev    next >
C/C++ Source or Header  |  1997-06-13  |  4KB  |  160 lines

  1. /* debugsh.c:  Simple debugging console
  2.  *
  3.  * This is free software, licensed under the GNU Public License V2.
  4.  * See the file COPYING for details.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <signal.h>
  11. #include <sys/time.h>
  12. #include <sys/types.h>
  13. #include "pi-source.h"
  14. #include "pi-socket.h"
  15. #include "pi-dlp.h"
  16. #include "pi-syspkt.h"
  17.  
  18. #ifdef HAVE_SYS_SELECT_H
  19. #include <sys/select.h>
  20. #endif
  21.  
  22. int done = 0;
  23.  
  24. void read_user(int sd)
  25. {
  26.   char line[256];
  27.   int l = read(fileno(stdin), line, 256);
  28.   if(l>0)
  29.     line[l-1] = 0;
  30.     
  31.   if(strcmp(line,"apps")==0) {
  32.     sys_RemoteEvent(sd, 1, 5,170, 0, 0,0,0); /* Set the pen down */
  33.     sys_RemoteEvent(sd, 0, 5,170, 0, 0,0,0); /* And then lift it up */
  34.   } else if(strcmp(line,"menu")==0) {
  35.     sys_RemoteEvent(sd, 1, 5,200, 0, 0,0,0); /* Set the pen down */
  36.     sys_RemoteEvent(sd, 0, 5,200, 0, 0,0,0); /* And then lift it up */
  37.   } else if(strcmp(line,"reboot")==0) {
  38.     RPC(sd, 1, 0xA08C, 2, RPC_End);
  39.   } else if(strcmp(line,"coldboot")==0) {
  40.     RPC(sd, 1, 0xA08B, 2, RPC_Long(0),RPC_Long(0),RPC_Long(0),RPC_Long(0), RPC_End);
  41.   } else if(strcmp(line,"numdb")==0) {
  42.     printf("Number of databases on card 0: %d\n",
  43.       RPC(sd, 1, 0xA043, 0, RPC_Short(0), RPC_End)
  44.     );
  45.   } else if(strcmp(line,"dbinfo")==0) {
  46.     long creator, type, appInfo, sortInfo, modnum, backdate, moddate, crdate, version, attr;
  47.     char name[32];
  48.  
  49.     int id = RPC(sd, 1, 0xA044, 0, RPC_Short(0), RPC_Short(0), RPC_End);
  50.                           
  51.     RPC(sd, 1, 0xA046, 0, RPC_Short(0), RPC_Long(id), 
  52.                           RPC_Ptr(name,32),
  53.                           RPC_ShortRef(attr), RPC_ShortRef(version), RPC_LongRef(crdate),
  54.                           RPC_LongRef(moddate), RPC_LongRef(backdate), RPC_LongRef(modnum),
  55.                           RPC_LongRef(appInfo), RPC_LongRef(sortInfo), RPC_LongRef(type),
  56.                           RPC_LongRef(creator), RPC_End);
  57.     
  58.     printf("The name of db 0 (LocalID %x) is %s\n", id, name);
  59.     
  60.   } else if(strcmp(line,"quit")==0) {
  61.     done=1;
  62.   } else if(l>1){
  63.     printf("unknown command '%s' (try 'apps', 'menu', 'coldboot', 'reboot', 'dbinfo', or 'quit')\n", line);
  64.   }
  65.     
  66.   if(!done) {
  67.     printf("debugsh>");  
  68.     fflush(stdout);
  69.   }
  70.   if(l==0)
  71.       done = 1;
  72. }
  73.  
  74. void read_pilot(int sd)
  75. {
  76.   char buf[4096];
  77.   int l = pi_read(sd, buf, 4096);
  78.   puts("From Pilot:");
  79.   dumpdata((unsigned char *)buf, l);
  80.   
  81.   if(buf[2] == 0) { /* SysPkt command */
  82.     if(buf[0] == 1) { /* Console */
  83.       if(buf[4] == 0x7f) { /* Message from Pilot */
  84.           int i;
  85.           for(i=6;i<l;i++)
  86.             if(buf[i] == '\r')
  87.               buf[i] = '\n';          
  88.         printf("%s", buf+6);
  89.       }
  90.     }
  91.   }
  92.  
  93.   if(!done) {
  94.     printf("debugsh>");  
  95.     fflush(stdout);
  96.   }
  97.  
  98. }
  99.  
  100. void sig(int signal) {
  101.   done = 1;
  102. }
  103.  
  104. int main(int argc, char *argv[])
  105. {
  106.   struct pi_sockaddr laddr;
  107.   int sd;
  108.   fd_set r,rin;
  109.   int max;
  110.  
  111.   if (argc < 2) {
  112.     fprintf(stderr,"usage:%s %s\n",argv[0],TTYPrompt);
  113.     exit(2);
  114.   }
  115.  
  116.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_RAW, PI_PF_SLP))) {
  117.     perror("pi_socket");
  118.     exit(1);
  119.   }
  120.     
  121.   laddr.pi_family = PI_AF_SLP;
  122.   strcpy(laddr.pi_device,argv[1]);
  123.   
  124.   pi_bind(sd, (struct sockaddr*)&laddr, sizeof(laddr));
  125.   
  126.   /* Now we can read and write packets: to get the Pilot to send a packet,
  127.      write a ".2" shortcut, which starts the debugging mode. (Make sure to
  128.      reset your Pilot after finishing this example!) */
  129.      
  130.   FD_ZERO(&r);
  131.   FD_SET(sd, &r);
  132.   FD_SET(fileno(stdin), &r);
  133.   
  134.   max = sd;
  135.   if(fileno(stdin)>max)
  136.       max = fileno(stdin);
  137.       
  138.   printf("debugsh>");
  139.   fflush(stdout);
  140.   
  141.   signal(SIGINT, sig);
  142.   
  143.   while(!done) {
  144.       rin = r;
  145.       if(select(max+1, &rin, 0, 0, 0)>=0) {
  146.           if(FD_ISSET(fileno(stdin),&rin)) {
  147.               read_user(sd);
  148.           } else if(FD_ISSET(sd,&rin)) {
  149.               read_pilot(sd);
  150.           }
  151.       } else {
  152.           break;
  153.       }
  154.   }    
  155.   
  156.   printf("\nExiting...\n");
  157.   pi_close(sd);
  158.   exit(0);
  159. }
  160.